package com.tianhui.consignortwofei.mvp.ui.activity.audit.driver;

import android.text.TextUtils;
import android.view.View;

import com.fgs.common.CommonFragment;
import com.fgs.common.CommonPresenter;
import com.fgs.common.CommonView;
import com.fgs.common.entity.PictureInfo;
import com.fgs.common.utils.DateUtil;
import com.fgs.common.widget.itemView.ClickItemView;
import com.fgs.common.widget.itemView.InputItemView;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.constant.KeyValue;
import com.tianhui.consignortwofei.mvp.model.enty.DriverInfo;
import com.tianhui.consignortwofei.mvp.model.enty.SelectPhotoInfo;
import com.tianhui.consignortwofei.other.auth.PictureContext;
import com.tianhui.consignortwofei.other.auth.inte.SelectPhotoCallback;
import com.tianhui.consignortwofei.other.auth.stragety.CertificationStrategy;
import com.xuexiang.xui.widget.picker.widget.TimePickerView;
import com.xuexiang.xui.widget.picker.widget.builder.TimePickerBuilder;
import com.xuexiang.xui.widget.picker.widget.listener.OnTimeSelectListener;

import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;

public class CertificationAuditActivity extends BaseDriverAuditActivity {

    @BindView(R.id.activity_certification_audit_nameInputItemView)
    InputItemView mNameInputItemView;
    @BindView(R.id.activity_certification_audit_numberInputItemView)
    InputItemView mNumberInputItemView;
    @BindView(R.id.activity_certification_audit_dateClickItemView)
    ClickItemView mDateClickItemView;
    @BindView(R.id.activity_certification_audit_remarkInputItemView)
    InputItemView mRemarkInputItemView;

    private TimePickerView mEndDatePickerView;
    private String mCertificationImageId;

    @Override
    public int getLayoutId() {
        return R.layout.activity_certification_audit;
    }

    @Override
    public CommonPresenter createPresenter() {
        return null;
    }

    @Override
    public CommonView createView() {
        return null;
    }

    @Override
    public CommonFragment createFragment() {
        return null;
    }

    @Override
    public void showDriverInfo(DriverInfo info) {

        mNameInputItemView.setContent(info.drivername);
        mNumberInputItemView.setContent(info.qualificationnumber);
        mDateClickItemView.setContent(info.qualificationendtime);
        mRemarkInputItemView.setContent(info.remarks);
        loadPicture(info.imgqualification);

        mCertificationImageId = info.qualification;
    }

    @Override
    public void init() {
        super.init();
        mNextButton.setText("保存并审核通过");
    }

    @Override
    protected void showOperateView(int operate) {
        if (operate == KeyValue.KEY_EDIT_OPERATE) {
            mSaveButton.setVisibility(View.VISIBLE);
            mNextButton.setVisibility(View.GONE);
        } else if (operate == KeyValue.KEY_AUDIT_OPERATE) {
            mSaveButton.setVisibility(View.GONE);
            mNextButton.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public DriverInfo getSaveInfo() {
        String number = mNumberInputItemView.getContent();
        String date = mDateClickItemView.getContent();
        if (TextUtils.isEmpty(number)) {
            showToast("请输入从业资格证号");
            return null;
        }
//        if (TextUtils.isEmpty(date)) {
//            showToast("请选择从业资格证有效期");
//            return null;
//        }

        String remark = mRemarkInputItemView.getContent();
        mDriverInfo.remarks = remark;

        mDriverInfo.qualificationnumber = number;
        mDriverInfo.qualificationendtime = date;
        mDriverInfo.qualification = mCertificationImageId;
        return mDriverInfo;
    }

    //有效期
    @OnClick(R.id.activity_certification_audit_dateClickItemView)
    public void selectEndDate() {
        hideKeyboard();
        if (mEndDatePickerView == null) {
            mEndDatePickerView = new TimePickerBuilder(this, new OnTimeSelectListener() {
                @Override
                public void onTimeSelected(Date date, View v) {
                    String string = DateUtil.getStandardTime(date.getTime(), "yyyy-MM-dd");
                    mDateClickItemView.setContent(string);
                }
            })
                    .setTitleText("请选择日期")
                    .build();
        }
        mEndDatePickerView.show();
    }

    @Override
    public void nextAudit() {
        DriverInfo driverInfo = getSaveInfo();
        if (driverInfo != null) {
            driverInfo.belongbank = "2";
            driverInfo.isaudit = 1;
            requestDriverSaveAndAudit(driverInfo);
        }
    }


    @Override
    public void changePictureSuccess(PictureInfo pictureInfo) {
        mCertificationImageId = pictureInfo.id;
    }

    @OnClick(R.id.activity_certification_audit_inputLicenseNumberImageView)
    public void inputCertificationNumber() {
        mNumberInputItemView.setContent(mDriverInfo.driverlicensenumber);
    }

    @OnClick(R.id.layout_audit_picture_editImageView)
    public void changePicture() {
        CertificationStrategy certificationStrategy = (CertificationStrategy) pictureStrategyFactory.createStrategy(CertificationStrategy.class);
        pictureContext = new PictureContext(certificationStrategy);
        pictureContext.selectPhoto(getContext(), new SelectPhotoCallback() {
            @Override
            public void selectPhotoSuccess(SelectPhotoInfo resultInfo) {
                mCertificationImageId = resultInfo.pictureId;
                loadPicture(resultInfo.realFileName);
            }

            @Override
            public void selectPhotoFailed() {

            }
        });
    }
}
